<?php
App::uses('Xml', 'Utility');
App::uses('File', 'Utility');
class SitemapController extends SitemapAppController {	
	public $levels1 = array();
	public $levels2 = array();	
	public $levels3 = array();
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index');
		$this->layout = 'xml';
	}
	
	public function index(){
		Configure::write('debug',0);
		$url = 'http://'.Configure::read('Config.domain');
		$this->generate(1,$url);
		foreach ($this->levels1 as $url => $l) {
			$this->generate(2,$url);
		}		
		foreach ($this->levels2 as $url => $l) {
			$this->generate(3,$url);
		}
		
		$txt = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
		foreach ($this->levels1 as $url => $level) {
		    $txt .= "<url><loc>{$url}</loc></url>";
		}
		foreach ($this->levels2 as $url => $level) {
			$txt .= "<url><loc>{$url}</loc></url>";
		}
		foreach ($this->levels3 as $url => $level) {
			$txt .= "<url><loc>{$url}</loc></url>";
		}
		$txt .= "</urlset>";
		
		$xml = Xml::build($txt);
		$xmlString = $xml->asXML();
		$file = new File('sitemap.xml');
		
		$file->write($xmlString);
		
		$this->redirect($this->referer());
	}
	
	
	private function generate($poziom = 1,$url){
		$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
		
		// make the cURL request to $target_url
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$html= curl_exec($ch);
		if (!$html) {
			echo "<br />cURL error number:" .curl_errno($ch);
			echo "<br />cURL error:" . curl_error($ch);
// 			exit;
		}else{
		
			$xml = new DOMDocument();
			// Load the url's contents into the DOM (the @ supresses any errors from invalid XML)
			if($xml->loadHTML($html)){
					
	
				$xpath = new DOMXPath($xml);
				$url_list = $xpath->evaluate("/html/body//a");
				;
				//Loop through each <a> and </a> tag in the dom and add it to the link array
	// 			$url_list = @$xml->getElementsByTagName('a');
					
				if(is_object($url_list)){
					foreach($url_list as $link) {
							
						$name = $link->nodeValue;
						$current_url = $link->getAttribute('href');
						$pattern0 =  '/mailto/i';
						$pattern1 =  '/'.Configure::read('Config.domain').'/i';
						if(!empty($name) && !preg_match($pattern0, $current_url) && preg_match($pattern1, $current_url) ){			
							if($poziom == 1){ if(!array_key_exists($current_url,$this->levels1))	$this->levels1[$current_url] = 0; }
							if($poziom == 2){ if(!array_key_exists($current_url,$this->levels1) && !array_key_exists($current_url,$this->levels2))	$this->levels2[$current_url] = 0; }
							if($poziom == 3){ if(!array_key_exists($current_url,$this->levels1) && !array_key_exists($current_url,$this->levels2) && !array_key_exists($current_url,$this->levels3))	$this->levels3[$current_url] = 0; }			
						}							
					}
				}
			}
		}
	}
}